<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>首页</el-breadcrumb-item>
      <el-breadcrumb-item>报告管理</el-breadcrumb-item>
      <el-breadcrumb-item>实验报告</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" class="user-search">
      <el-form-item label="搜索：">
        <el-input size="small" v-model="searchName" @input="inputAlter" placeholder="学生姓名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">
          搜索
        </el-button>
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table size="small" :data="
      dataList.slice(
        (this.pageparm.currentPage - 1) * this.pageparm.pageSize,
        this.pageparm.currentPage * this.pageparm.pageSize
      )
    " highlight-current-row v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%">
      <el-table-column sortable prop="title" label="报告名称">
      </el-table-column>
      <el-table-column sortable prop="name" label="学生姓名">
      </el-table-column>
      <el-table-column sortable prop="sno" label="学号">
      </el-table-column>
      <el-table-column sortable prop="grade" label="班级"> </el-table-column>
      <el-table-column sortable prop="algorithm" label="算法"> </el-table-column>
      <el-table-column sortable prop="comment" label="批改"> </el-table-column>
      <el-table-column sortable min-width="110px" prop="created_at" label="创建时间"> </el-table-column>
      <el-table-column align="center" min-width="128px" label="查看">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleViewer(scope.row.id)">查看/下载</el-button>
          <el-button size="mini" @click="handleReview(scope.row)">批改</el-button>
          <el-button size="mini" type="danger" @click="deleteReport(scope.$index, scope.row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
    <el-dialog :visible.sync="editFormVisible" title="批改意见" @click="closeDialog" width="50%">
      <el-form label-width="120px" :model="editRow" ref="editRow">
        <el-form-item label="批改意见" prop="comment">
          <el-input v-model="editRow.comment" type="textarea" :rows="5" placeholder="请填写意见"></el-input>
        </el-form-item>
      </el-form>
      <div class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button size="small" type="primary" class="title" @click="submitEdit('editRow')">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import Pagination from "../../components/Pagination";

  export default {
    data() {
      return {
        loading: false, //是显示加载
        editFormVisible: false, //控制批改页面显示与隐藏
        searchName: "", //搜索名称
        dataList: [], //用户数据
        // 分页参数
        pageparm: {
          currentPage: 1,
          pageSize: 10,
          total: 10,
        },
        // 批改意见
        editRow: {
          id: "",
          comment: ""
        },
        // 提交按钮显隐
        submitButton: true,
      };
    },
    // 注册组件
    components: {
      Pagination,
    },
    /**
     * 创建完毕
     */
    created() {
      this.getDataList();
    },

    /**
     * 里面的方法只有被调用才会执行
     */
    methods: {
      // 获取公司列表
      getDataList() {
        this.loading = true;
        this.$axios.get("/reports/").then((res) => {
          this.loading = false
          this.dataList = res.data.results.list;
          this.pageparm.total = res.data.results.total;
        }).catch(err => {
          this.loading = false
          this.$message.error('报告加载失败，请稍后再试！')
        });
      },
      // 分页插件事件
      callFather(parm) {
        this.pageparm.currentPage = parm.currentPage;
        this.pageparm.pageSize = parm.pageSize;
      },
      // 输入框内容改变
      inputAlter() {
        if (this.searchName == "") {
          this.getDataList();
        } else {
          this.dataList = this.dataList.filter((value) => {
            return value.name.match(this.searchName);
          });
          this.pageparm.total = this.dataList.length;
        }
      },
      // 搜索事件
      search() {
        this.dataList = this.dataList.filter((value) => {
          return value.name.match(this.searchName);
        });
        this.pageparm.total = this.dataList.length;
      },
      // 编辑保存
      submitEdit() {
        this.loading = true;
        this.$axios.post("/reports/info/" + this.editRow.id + "/", this.editRow)
          .then((res) => {
            this.editFormVisible = false;
            this.loading = false;
            this.getDataList();
          })
          .catch((err) => {
            this.editFormVisible = false;
            this.loading = false;
            this.$message.error("批改意见更新失败，请稍后再试！");
          });
      },
      // 删除
      deleteReport(index, row) {
        this.$confirm("确定要删除吗?", "信息", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            this.$axios
              .post("/reports/delete/" + row.id + "/")
              .then((res) => {
                this.$message({
                  type: "success",
                  message: "报告已删除!",
                });
                this.getDataList();
              })
              .catch((err) => {
                this.loading = false;
                this.$message.error("删除报告失败，请稍后再试！");
              });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      },
      // 查看报告
      handleViewer(rptId) {
        this.$router.push({ path: "/ReportView", query: { rptId } });
      },
      // 显示批改页
      handleReview(row) {
        this.editFormVisible = true;
        this.editRow.id = row.id;
        this.editRow.comment = row.comment;
      },

      // 关闭编辑、增加弹出框
      closeDialog() {
        this.editFormVisible = false;
      },
      // 提交报告
      submit() {
        this.submitButton = false;
      },
    },
  };
</script>

<style scoped>
  .user-search {
    margin-top: 20px;
  }

  .userRole {
    width: 100%;
  }

  .dialog-footer {
    display: flex;
    justify-content: center;
  }
</style>